//
//  IntroduceViewController.m
//  Travel
//
//  Created by lanou3g on 15/12/15.
//  Copyright © 2015年 涂晓雨. All rights reserved.
//

#import "IntroduceViewController.h"
#import "PlayModel.h"
#import "PlayDataHelper.h"
#import "detailModel.h"
@interface IntroduceViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)BOOL isExpand;
@end

@implementation IntroduceViewController 

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds ];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.isExpand = NO;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"景区指南";
    }else{
      return @"简介";
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isExpand && indexPath.section == 1) {
        
        self.detailModel = self.array[0];
        NSString *str = self.detailModel.Introduction;
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:19]};
        CGRect labelFrame = [str boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 20000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
        
        return labelFrame.size.height;
    }else{
       return 250;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return  30;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    self.detailModel = self.array[0];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (indexPath.section == 0) {
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.text = [NSString stringWithFormat:@"门票: %@\n\n类型: %@\n\n游玩旺季: %@\n\n开放时间: %@\n\n地址: %@",self.detailModel.TicketsPrice,self.detailModel.TourType,self.detailModel.HotSeason,self.detailModel.OpenTime,self.detailModel.Address];
    }

    if (indexPath.section == 1) {
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.text = [NSString stringWithFormat:@"\n\n\t%@",self.detailModel.Introduction];
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
        button.backgroundColor = [UIColor redColor];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 20;
        [cell.textLabel addSubview:button];
        cell.textLabel.userInteractionEnabled = YES;
    }
 
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!indexPath.row == 1) {
        self.isExpand = YES;
           [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }else{
    
        if (self.isExpand) {
            if (indexPath.row == 1) {
                self.isExpand = NO;
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }else{
            
                self.isExpand = NO;
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
        }
    
    }

}
@end
